<?php

/*
	Class: Commerce_Plugins_Checkout

	About: Author
		Tyler Vigeant

	About: License
		<http://communit.as/docs/license>
*/
class Commerce_Plugins_Analytics {

	/* Group: Constructors */

	/*
		Function: __construct
	*/
	function __construct() {
	}

	/* Group: Instance Methods */

	/*
		Function: addData
	*/
	function addData($params) {
		memory_profiler("start Commerce Google Analytics Plugin");
		
		$params['analytics'] = array();
		$params['analytics']['error'] = false;
		
		$request = new Cts_Request($params['request']);

			Zend_Session::start();
			$qaSession = new Zend_Session_Namespace('testing');
		if ($request->qa) {
			$qaSession->GA_off = "true";
		}
 		
		if( Cts_Registry::get('analytics_is_enabled')=='1' && !$qaSession->GA_off){
			## Enable Google Analytics
			$params['analytics']['on'] = true;
			
			## Enable Roll Up (one account for all locales)
			if( Cts_Registry::get('analytics_use_roll_up') ){
				$params['analytics']['rollup'] = Cts_Registry::get('analytics_roll_up_tracking_code');
			} else {
				$params['analytics']['rollup'] = false;
			}
			
			$codes = explode(',', str_replace(' ','', Cts_Registry::get('analytics_tracking_codes')));
			$locales = explode(',', str_replace(' ','', Cts_Registry::get('analytics_tracking_locales')));
			
			## Apply the right tracking code for this locale
			if( count($codes) == count($locales) ){
				if( Cts_Registry::get('enable_localization')=='1' && isset($params['locale_code']) ){
					for( $x=0; $x<count($codes); $x++ ){
						if( $locales[$x] == $params['locale_code'] ){
							$params['analytics']['track'] = $codes[$x];
							break;
						}
					}
				}
			} else {
				Cts_Log::report("ANALYTICS CONFIGURATION ERROR: The number of tracking codes and locales do not match. Please seperate codes and locales with a comma. To use the same tracking code for multiple locales, please enter it multiple times.", NULL, Zend_Log::ERR);
				$params['analytics']['on'] = false;
				$params['analytics']['error'] = true;
				return $params;
			}
			
			## Default tracking code
			if( !isset($params['analytics']['track']) ){
				$params['analytics']['track'] = $codes[0];
			}
			
			## Other variables
			$params['analytics']['domain'] = Cts_Registry::get('analytics_domain');
			$params['analytics']['track_commerce'] = (Cts_Registry::get('analytics_track_commerce')=='1') ? true : false;
			$params['analytics']['track_events'] = (Cts_Registry::get('analytics_track_events')=='1') ? true : false;
		} else {
			$params['analytics']['on'] = false;
		}
		
		memory_profiler("end Commerce Google Analytics Plugin");
		
		return $params;
	}

}
